package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.Model.AnswerModel;
import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionType;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class QuestionBankServiceWqjImpl extends QuestionBankServiceImpl implements QuestionBankService {
    @Override
    public List<Question> importQuestions(String questionTextFile) {
        String fileContent = getQuestionFileContent();
        String[] file = fileContent.split("\n");
        QuestionType[] types = QuestionType.values();
        List<Question> questionList = new ArrayList<>();
        boolean isCorrect = false;
        int index = 0;
        for (int i = 0; i < file.length; i = i + 6) {
            String content = file[i].substring(file[i].indexOf("、") + 1);
            Question question = new Question(index + 1, content, types[0]);
            for (int j = 1; j < 5; j++) {
                String answer = file[i + j].substring(0, 1);
                if (log.isDebugEnabled()) log.debug(file[i + j]);
//Option(String answer, char choice, boolean isCorrectAnswer)
                String choice = file[i + j].substring(2);
                char[] cho = choice.toCharArray();
                Option option = new Option(answer, cho[0], isCorrect);
                question.add(option);
            }
            index++;
            questionList.add(question);
        }
        //}
        return questionList;
    }

    @Override
    public List<Question> importCorrectAnswers(String correctAnswerTextFile) {
        //对每道题正确或者错误进行判断
        Map<Integer, Character> correctAnswerMap = readCorrectAnswer3("");
        List<Question> questionList = importQuestions("");

        for (Question question : questionList) {
            correctAnswerMap.containsKey(question.getNo());//先判断是否有
            char choice = correctAnswerMap.get(question.getNo());//放是put，取是get
            Option option = question.getOptions().stream()
                    .filter(option1 -> option1.getChoice() == choice)
                    .findFirst()
                    .orElseThrow(() -> new IllegalArgumentException("没有找到正确选项"));
            option.changeToCorrectAnswer();
        }

        return questionList;
    }

    @Override
    public List<AnswerModel> readCorrectAnswers(String correctAnswerTextFile) {
        String fileContent = getAnswerFileContent();
        List<AnswerModel> answerList = new ArrayList<>();
        String[] list = fileContent.split("\n");
        for (int i = 0; i < list.length; i = i + 2) {
            String[] questionNo = list[i].split("\t");
            String[] questionAnswer = list[i + 1].split("\t");
            for (int j = 0; j < questionNo.length; j++) {
                AnswerModel answer = new AnswerModel(Integer.parseInt(questionNo[j]), questionAnswer[j].charAt(0));
                answerList.add(answer);
            }
        }
        return answerList;
    }

    //正确答案存为 Map
    public Map<Integer, Character> readCorrectAnswers2(String correctAnswerTextFile) {
        Map<Integer, Character> result = new HashMap<>();
        String[] lines = getAnswerFileContent().split("\n");
        for (int i = 0; i < lines.length; i += 2) {
            String[] nos = lines[i].split("\t");
            String[] choices = lines[i + 1].split("\t");
            for (int j = 0; j < nos.length; j++) {
                String no = nos[j];
                String choice = choices[j];
                result.put(Integer.parseInt(no), choice.charAt(0));
            }
        }
        return result;
    }

    //Map就是存键值对
    public Map<Integer, Character> readCorrectAnswer3(String correctAnswerTextFile) {
        Map<Integer, Character> correctAnswerMap = new HashMap<>();
        String[] answerFile = getAnswerFileContent().split("\n");
        for (int i = 0; i < answerFile.length; i += 2) {
            String[] questionNo = answerFile[i].split("\t");
            String[] choice = answerFile[i + 1].split("\t");
            for (int j = 0; j < questionNo.length; j++) {
                String no = questionNo[j];
                String cho = choice[j];
                //转换成Map需求的类型
                correctAnswerMap.put(Integer.getInteger(no), cho.charAt(0));
            }
        }
        return correctAnswerMap;
    }

    @Override
    public List<Question> importCategories(String categoryExcelFile) {

        return null;
    }
}
